using MediatR;
using SumerCoreDevOps.Application.Roles.Queries;
using SumerCoreDevOps.Domain.Entities;
using SumerCoreDevOps.Infrastructure.Identity;

namespace SumerCoreDevOps.Application.Roles.Handlers;

public class GetRoleByIdQueryHandler : IRequestHandler<GetRoleByIdQuery, Role?>
{
    private readonly RoleManager _roleManager;

    public GetRoleByIdQueryHandler(RoleManager roleManager)
    {
        _roleManager = roleManager;
    }

    public async Task<Role?> Handle(GetRoleByIdQuery request, CancellationToken cancellationToken)
    {
        return await _roleManager.GetRoleByIdAsync(request.RoleId);
    }
}

public class GetAllRolesQueryHandler : IRequestHandler<GetAllRolesQuery, List<Role>>
{
    private readonly RoleManager _roleManager;

    public GetAllRolesQueryHandler(RoleManager roleManager)
    {
        _roleManager = roleManager;
    }

    public async Task<List<Role>> Handle(GetAllRolesQuery request, CancellationToken cancellationToken)
    {
        return await _roleManager.GetAllRolesAsync(request.IncludePermissions);
    }
}

public class GetUsersByRoleIdQueryHandler : IRequestHandler<GetUsersByRoleIdQuery, List<User>>
{
    private readonly RoleManager _roleManager;

    public GetUsersByRoleIdQueryHandler(RoleManager roleManager)
    {
        _roleManager = roleManager;
    }

    public async Task<List<User>> Handle(GetUsersByRoleIdQuery request, CancellationToken cancellationToken)
    {
        return await _roleManager.GetUsersByRoleIdAsync(request.RoleId);
    }
}
